package cn.wolfcode.wolf2w.redis.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.common.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyInfoRedisServiceImpl implements IStrategyRedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void increaseViewnum(String id, int i) {
        StrategyStatisVO strategyStatisVO = this.queryStatisVo(id);
        //统一增加1
        strategyStatisVO.setViewnum(strategyStatisVO.getViewnum() + i);
        //更新
        this.setStrategyStatisVO(strategyStatisVO );
    }

    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Override
    public StrategyStatisVO queryStatisVo(String sid) {
        //拼接vo对象的key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断redis中是否有key
        StrategyStatisVO vo = new StrategyStatisVO();
        if (template.hasKey(key)) {
            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        } else {
            Strategy strategy = strategyService.get(sid);
            vo.setStrategyId(strategy.getId());
            BeanUtils.copyProperties(strategy, vo);
            template.opsForValue().set(key, JSON.toJSONString(vo));
        }
        template.opsForValue().set(key, JSON.toJSONString(vo));
        return vo;
    }


    @Override
    public void increaseReply(String strategyId, int num) {
        StrategyStatisVO strategyStatisVO = this.queryStatisVo(strategyId);
        strategyStatisVO.setReplynum(strategyStatisVO.getReplynum()+num);
        this.setStrategyStatisVO(strategyStatisVO);
    }

    @Override
    public boolean  favor(String sid,String uid) {
        StrategyStatisVO vo = this.queryStatisVo(sid);
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> sidList = new ArrayList<>();
        if(template.hasKey(key)){
            //如果存在则直接获取当前key下面的sidList
            String strSidList = template.opsForValue().get(key);
            sidList = JSON.parseArray(strSidList,String.class);
        }
        //判断sidList中是否包含目前攻略的id
        if(sidList.contains(sid)){
            //取消操作
            vo.setFavornum(vo.getFavornum()-1);
            sidList.remove(sid);
        }else{
            //收藏操作
            vo.setFavornum(vo.getFavornum()+1);
            sidList.add(sid);
        }
        //更新记号 ,更新vo
        template.opsForValue().set(key,JSON.toJSONString(sidList));
        this.setStrategyStatisVO(vo);
        return sidList.contains(sid);
    }

    @Override
    public List<String> getSids(String userId) {
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        List<String> list= new ArrayList<>();
        if(template.hasKey(key)){
            String sids = template.opsForValue().get(key);
            list = JSON.parseArray(sids, String.class);
        }
        return list;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //拼接记号key
        String key = RedisKeys.USER_STRATEGY_THUMBUP.join(uid,sid);
        //redis中有则代表已经点赞
        if(!template.hasKey(key)){
            StrategyStatisVO vo = this.queryStatisVo(sid);
            Date date = new Date();
            //每天最后一秒
            Date endDate = DateUtil.getEndDate(date);
            //时间差
            long timeOut = DateUtil.getDateBetween(date,endDate);
            vo.setThumbsupnum(vo.getThumbsupnum()+1);
            this.setStrategyStatisVO(vo);
            template.opsForValue().set(key,"1",timeOut,TimeUnit.SECONDS);
            return true ;
        }
        return false;
    }

    @Override
    public boolean isStrategyVoExist(String strategyId) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(strategyId);
        return   template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> getStaregyVos() {
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");
        Set<String> keys = template.keys(pattern);
        List<StrategyStatisVO> list = new ArrayList<>();
        if(keys!=null && keys.size()>0){
            for (String key : keys) {
                String strVo = template.opsForValue().get(key);
                list.add(JSON.parseObject(strVo, StrategyStatisVO.class));
            }
        }
        return list;
    }
}















   /* @Override
    public StrategyStatisVO increaseReply(String strategyId,int num) {
        //拼接vo对象的key
        String key =RedisKeys.USER_STRATEGY_REPLY.join(strategyId);
        String key1 =RedisKeys.STRATEGY_STATIS_VO.join(strategyId);
        vo.setReplynum(vo.getReplynum()+num);
        //更新
        template.opsForValue().set(key1, JSON.toJSONString(vo));
        template.opsForValue().set(key, JSON.toJSONString(vo));
        return vo;
    }
}
*/